<?php

abstract class Theme_Exception extends CMS_Exception {
	public function __construct($theme){
		$this->_theme = $theme;
		parent::__construct();
	}
	public function __toString(){
		return "{$this->_theme}\r\n" . parent::__toString();
	}
}
abstract class Theme_Class_Exception extends Theme_Exception {
	public function __construct($theme, $class){
		$this->_class = $class;
		parent::__construct($theme);
	}
	public function __toString(){
		return "{$this->_class}\r\n" . parent::__toString();
	}
}
class Theme_Class_Not_Found_Exception extends Theme_Class_Exception {}
class Theme_Class_Invalid_Exception extends Theme_Class_Exception {}
class Theme_Invalid_Exception extends Theme_Exception {}

abstract class Theme {

	protected $name;
	private static $instance;

	public function __construct($name){
		$this->name = $name;
	}

	public function Template($tpl, $data){
		array_unshift($tpl,'theme',$this->name);
		$data['theme'] = $this;
		$template = Template::Create($tpl, $data);
		return $template;
	}

	public static function getInstance(){
		if(!isset(Theme::$instance)){
			throw new Theme_Invalid_Exception("");
		}
		return Theme::$instance;
	}

	public static function Load($name = null){
		if(isset($name)){
			$class = "Theme_{$name}";
			return new $class($name);
		}
		$name = Site::getInstance()->getValue('theme');
		CMS::File("theme/{$name}/theme");
		$class = "Theme_{$name}";
		if(!class_exists($class)){
			throw new Theme_Class_Not_Found_Exception($name, $class);
		}
		if(!is_subclass_of($class, "Theme")){
			throw new Theme_Class_Invalid_Exception($name, $class);
		}
		Theme::$instance = call_user_func(array($class, "Load"), $name);
	}




}
